package com.lyl.lawyer.util;


import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

public class Upload {
    /*  public static void  file(HttpServletRequest req){

          DiskFileItemFactory factory = new DiskFileItemFactory();

          // 设置 缓存的大小，当上传文件的容量超过该缓存时，直接放到 暂时存储室
          factory.setSizeThreshold(1024 * 1024);
          // 高水平的API文件上传处理,解析文件
          ServletFileUpload upload = new ServletFileUpload(factory);

          List<FileItem> list = null;
          try {
              list = upload.parseRequest(req);
          } catch (FileUploadException ex) {
              ex.printStackTrace();
          }

          // 获取上传的文件
          for (FileItem file : list) {
              FileItem item = file;
              System.out.println(item.getFieldName());
              System.out.println(item.getName());
              String name = item.getName();
             //添加时间戳防止重复
              String fileName = System.currentTimeMillis() + "---" + name;
              String temp = req.getServletContext().getRealPath("/");//服务器路径
              System.out.println(temp);
              String path = temp.split("out")[0] + "/web/";//真实路径
              String directory = "/img/";

//            文件夹不存在则创建文件夹
              File aa = IO.createDirectory(path + directory);
              try {
                  File tempFile = new File(aa, fileName);
                  item.write(tempFile);
                  File newFile = IO.createDirectory(temp + directory);
                  copy(tempFile, new File(newFile, fileName));
                  copy(tempFile,new File("D:/新建文件夹/folder/jsp/02/web/img/",fileName));
                  copy(tempFile,new File("D:/新建文件夹/folder/jsp/02/out/artifacts/02_war_exploded/img",fileName));
                  req.setAttribute("imgHref", directory + fileName);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
*/
    public static File copy(MultipartFile file, String newDest, String newName) throws IOException {

        File newFile=new File(newDest,newName);
    FileUtils.copyInputStreamToFile(file.getInputStream(),newFile);
    return newFile;
    }
}
